package com.liveims.webims;


import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.log4j.Logger;

import com.liveims.webims.util.RestPath;
import com.liveims.webims.util.Settings;
import com.nextenso.proxylet.ProxyletConfig;
import com.nextenso.proxylet.ProxyletException;
import com.nextenso.proxylet.http.BufferedHttpRequestProxylet;
import com.nextenso.proxylet.http.HttpClient;
import com.nextenso.proxylet.http.HttpClientFactory;
import com.nextenso.proxylet.http.HttpHeaders;
import com.nextenso.proxylet.http.HttpRequest;
import com.nextenso.proxylet.http.HttpRequestProlog;
import com.nextenso.proxylet.http.HttpResponse;
import com.nextenso.proxylet.http.HttpResponseProlog;



/*
 * This proxylet is responsible to handle the HTTP request to '/IMPU/profile' and '/IMPU/contacts',
 * which are static resources (stateless)
 * 
 */
public class StaticProxylet implements BufferedHttpRequestProxylet {
	
	private static String XcapProfileURL;
	private static String XcapContactListURL; 
	
	private static final Logger log = Logger.getLogger(StaticProxylet.class);
	
	RestPath restPath = null;
	

	public int doRequest(HttpRequest request) throws ProxyletException {
		
		HttpResponse response = request.getResponse();
		HttpResponseProlog respProlog = response.getProlog();
		HttpRequestProlog reqProlog = request.getProlog(); 
		
		String _impu = restPath.getIMPU();
		//String user_sipuri = "sip:"+_impu+"@"+Settings.get_ims_domain();
		String user_sipuri = "sip:"+_impu;
		
		XcapProfileURL = "http://" + Settings.getInstance().valueOf(Settings.Attribute.XDMS_PROPERTY) + "/services/org.openmobilealliance.user-profile/users/webims.impu/user-profile";
		XcapContactListURL = "http://" + Settings.getInstance().valueOf(Settings.Attribute.XDMS_PROPERTY) + "/resource-lists/users/webims.impu/index";
		
		//former the request URL
		String XcapURL = null;
		if(restPath.isForProfile())
		   XcapURL = XcapProfileURL.replaceAll("webims.impu", user_sipuri);
		else if(restPath.isForContacts())
		   XcapURL = XcapContactListURL.replaceAll("webims.impu", user_sipuri);
		
		//to do the request
		if(XcapURL != null){
		    if (reqProlog.getMethod().equalsIgnoreCase("GET")){    
			   try{
			       HttpClient httpclient = HttpClientFactory.newHttpClient(XcapURL);
			       log.info("sending GET to "+XcapURL);
			       int respCode = httpclient.doGET();
			       if (respCode == 200 || respCode ==201 || respCode == 304 ){
			    	   respProlog.setStatus(200);
			    	   respProlog.setReason("OK");
			    	}
			       if (httpclient.getResponse()!=null){
			         response.getBody().setContent(new String(httpclient.getResponse()));
			         //TODO replace the text/xml by text/atom
			         
			       }
		      }catch(MalformedURLException e1){
			    log.error(e1);
			  }catch(IOException e2){log.error(e2);}
		      
		    }else if(reqProlog.getMethod().equalsIgnoreCase("PUT")){
		   //TODO update static docs 	
		    }
		}

		return  RESPOND_FIRST_PROXYLET;
	}

	public int accept(HttpRequestProlog prolog, HttpHeaders headers) {
        String path = prolog.getURL().getPath();
        restPath = new RestPath(path);
        if (restPath.isForContacts()||restPath.isForProfile())
		  return ACCEPT_MAY_BLOCK;
        else
          return IGNORE;
	}

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public String getProxyletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	public void init(ProxyletConfig cnf) throws ProxyletException {

	}

}
